package com.qztech.utils;

import android.util.Log;

/**
 * Copyright (C),
 * Carate By YangZhong
 */
public class YzDebug {
	public static String TAG = "Dsp";
	public static boolean isDebug = true;
	public static void d(String tag, String msg){
		if(isDebug)
            handleLog(Log.DEBUG,tag, msg, null);
	}
	public static void d(String msg, Throwable e){
        if(isDebug)
            handleLog(Log.DEBUG,null, msg, e);
    }
	public static void d(String tag, String msg, Throwable e)
	{
		if(isDebug)
            handleLog(Log.DEBUG,tag, msg, e);
	}

	public static void d(String msg){
		if(isDebug)
			handleLog(Log.DEBUG,null,msg , null);
//		Log.d(TAG, msg);
	}
	public static void e(String msg)
	{
		if(isDebug)
			handleLog(Log.ERROR,null,msg , null);
//		Log.e(TAG, msg);
	}
	public static void d(Throwable e)
	{
		if(isDebug)
            handleLog(Log.DEBUG,null,null, e);
	}
	public static void e(Throwable e)
	{
		if(isDebug)
            handleLog(Log.ERROR,null,null, e);
	}
	private static void handleLog(int level, String head,String msg, Throwable e)
	{
		StackTraceElement[] stackslist = Thread.currentThread().getStackTrace();
		StackTraceElement tracke = null;

		if (stackslist != null && stackslist.length > 5){
			tracke = stackslist[4];
		}

		StringBuilder buffer = new StringBuilder();
		if (tracke != null)
		{
			buffer.append('[');
			String fName = tracke.getFileName();
			if (fName == null)
			{
				buffer.append("Unknown ");
			}
			else
			{
				int lineNum = tracke.getLineNumber();
				buffer.append(fName);
				if (lineNum >= 0)
				{
					buffer.append(':');
					buffer.append(lineNum);
				}
			}
			buffer.append(']');
		}

		if (head == null)
		{
			head = TAG;
		}

		if (msg != null)
		{
			buffer.append(msg);
		}

		switch(level)
		{
			case Log.VERBOSE:
				Log.v(head, buffer.toString(),e);
				break;

			case Log.DEBUG:
				Log.d(head, buffer.toString(),e);
				break;

			case Log.INFO:
				Log.i(head, buffer.toString(),e);
				break;

			case Log.WARN:
				Log.w(head, buffer.toString(),e);
				break;

			case Log.ERROR:
				Log.e(head, buffer.toString());
				break;

			default:
				break;
		}
	}


}
